<?php
namespace controllers;
use controllers\main;
use SDF\Db\Redis;
use SMB\Statistics\shop_sales_statistics;



class api extends main{
    public function success($data,$date='',$res='',$arr=''){
        echo json_encode(array('status'=>true,'data'=>$data,'date'=>$date,'res'=>$res,'arr'=>$arr));
    }
    public  function error($msg){
        echo json_encode(array('status'=>true,'error'=>$msg));
    }
    //一个月内的销售量统计
    public function one_month_num(){
        $date_arr =array();
        $shop_sales_statistics_model = new shop_sales_statistics();
        $redis = new Redis();
        $date = json_decode($redis->get('sale_num'));
        if(!$date){
            for($i=0;$i<30;$i++){
                $time = (int)date('Ymd',time()-($i*86400));
                $sales_all = $shop_sales_statistics_model->aggregate(array(
                    array(
                        '$match'=>array('Ymd'=>$time)
                    ),
                    array(
                        '$group'=>array(
                            '_id'=>array(
                                'sku_id'=>'$sku_id'
                            ),

                            'sales_money'=>array('$sum'=>'$sales_money'),
                            'sku_id'=>array('$first'=>'$sku_id')
                        )
                    )
                ));
                $sales_all_arr  = array();
                $arr = 0;
                foreach ($sales_all['result'] as $item){
                    $sales_all_arr[$item['sku_id']]['sales_money'] = $item['sales_money'];
                    $arr += $item['sales_money'];
                }
                array_push($date_arr,$arr);
            }
            $redis->set('sale_num',json_encode($date_arr),86400);
            $date = $date_arr;
        }

        return $date;
    }


}